package com.shanzhe.eshop.common.jdk;

import lombok.SneakyThrows;
import lombok.Synchronized;
import org.apache.tomcat.util.net.SocketBufferHandler;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author yinwei
 * @create 2021/10/13
 * @Version 1.0
 */
public class ReentractLockDemo {

    private static volatile int data = 0;
    private static ReentrantLock lock = new ReentrantLock(true);
    public static void main(String[] args) {
        try {
            new Thread(){
                @SneakyThrows
                @Override
                public void run() {
                    for(int i = 0;i<10;i++){
                        lock.lock();
                        ReentractLockDemo.data++;
                        lock.newCondition().await();
                        System.out.println(ReentractLockDemo.data);
                        lock.unlock();
                    }


                }
            }.start();


            new Thread(){
                @Override
                public void run() {
                    lock.lock();
                    ReentractLockDemo.data++;
                    lock.newCondition().signal();
                    System.out.println(ReentractLockDemo.data);
                    lock.unlock();
                }
            }.start();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
